package com.winhxd.b2c.customer.api;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.context.CustomerUser;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.customer.condition.MiniProgramSharePicCondition;
import com.winhxd.b2c.customer.service.GraphicsGenerateService;
import com.winhxd.b2c.customer.util.ValidateCustomerUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 图片生成类
 * @author shumin
 * @create 2019/1/14 0014 10:56
 */
@Api(value = "GraphicsGenerate Controller", tags = "graphics-generate")
@RestController
@RequestMapping(value = "/api-customer/", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ApiGraphicsGenerateController extends ValidateCustomerUser {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApiGraphicsGenerateController.class);

    @Autowired
    private GraphicsGenerateService graphicsGenerateService;

    @ApiOperation(value = "生成商品详情页的分享海报")
    @ApiResponses({ @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1002, message = "登录凭证无效")})
    @RequestMapping(value = "graphics/2080/v1/getProductSharePic", method = RequestMethod.POST)
    public ResponseResult<String> getProductSharePic(@RequestBody MiniProgramSharePicCondition condition) {
        ResponseResult<String> responseResult = new ResponseResult<>();
        LOGGER.info("生成商品详情页的分享海报 - 参数：condition={}", condition.toString());
        CustomerUser user = validateCustomerUser();
        condition.setCustomerId(user.getCustomerId());
        String url = graphicsGenerateService.generateMiniProgramProductSharePic(condition);
        responseResult.setData(url);
        LOGGER.info("生成商品详情页的分享海报 - 结果：图片url={}", url);
        return responseResult;
    }
}
